from heapq import *
# To change this template, choose Tools | Templates
# and open the template in the editor.


class ScheduleManager:
    def __init__(self):
        self.queue = []
        self.current_tick = 0
        self.ready = 0

        #this is the base recovery time for a unit. The speed of the unit
        #is deducted from this so that faster units move more often
        self.base_rt = 100


    def add_char(self, char, pen=0):
        rt = self.base_rt - char.attribute["spd"].use_val()
        rt += pen
        ipos = rt + self.current_tick
        char.tick = ipos
        heappush(self.queue, (ipos, char))

    def rep_char(self, char, pen=0):
        rt = self.base_rt - char.attribute["spd"].use_val()
        rt += pen
        ipos = rt + self.current_tick
        char.tick = ipos
        #print ipos
        heapreplace(self.queue, (ipos, char))
        #print 'thing is now at {0}'.format(ipos)
        return self.peek()

#    def next(self):
#        t, obj = heappop(self.queue)
#        while obj.dead:
#            t, obj = heappop(self.queue)
#        self.current_tick = t
#        return obj

    def peek(self):
        t, obj = self.queue[0]
        while obj.dead:
            print 'deaden'
            heappop(self.queue)
            t, obj = self.queue[0]
        self.current_tick = t
        return obj